﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.OA;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.OA.Pages
{
    public partial class DocFile_Upload : SsTemplatePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String pid = Request.QueryString["parentId"] ?? String.Empty;
            if (!IsPostBack)
            {
                this.txt_parentId.Text = pid;
            }
        }

        protected void linkbtn_UpLoad_Click(object sender, EventArgs e)
        {
            String pid = Request.QueryString["parentId"] ?? String.Empty;
            
            DocItem item = new DocItem();
            item.Id = Guid.NewGuid();
            item.ParentId = new Guid(pid);
            item.Code = this.txt_Code.Text;
            item.Caption = this.txt_Caption.Text;
            item.Description = this.txt_Description.Text;
            item.CreatedDate = DateTime.Now;
            item.Creator = this.User.Identity.Name;
            item.TypeCode = DocItem.File;

            DocFile doc = new DocFile();
            doc.Data = this.fileLoad.FileBytes;
            doc.Id = item.Id;
            OAService service = new OAService();
            ServiceResult resultFile = service.SaveDocFile(doc);

            ServiceResult resultItem = service.SaveDocItem(item);

            if (resultFile.IsSuccess && resultItem.IsSuccess)
            {
                Response.Redirect("DocItem-List.aspx?parentId=" + pid);
            }
        }
    }
}